#!/bin/sh
#
# File:   setjvm
# Author: Steve Giovannetti <stevegio@gmail.com>
# Purpose:
#          A simple shell program which performs 3 tasks:
#
#          1) reports all the JVM versions installed and 
#             highlights the current default JVM
#          2) reports only the JVM version of the current 
#             default JVM
#          3) allows the user to set the current JVM. You 
#             must run as root or under sudo to set the JVM.
# 
#         Copyright 2005 Steve Giovannetti
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

# Important file location
FM_DIR=/System/Library/Frameworks/JavaVM.framework/Versions

usage() { 

	echo "USAGE: `basename $0` [ -h ]" 
	echo "       sudo `basename $0` [ -v jdk_version ]" 

}

while getopts chv: OPTION; do
      case ${OPTION} in
           c) CURRENT_JVM=1;;
      	   v) NEWVERSION=${OPTARG}
	      if [ "`whoami`" != "root" ]; then
                 echo "Error: must be root to set default JVM version."
		 exit 2
	      fi;;
           h) usage
	      exit 0;;
          \?) usage
	      exit 2;;
      esac
done

unset versionlist

for f in `find $FM_DIR -name [0-9]\* -type d -maxdepth 1`; do
    v=`basename $f`
    versionlist="$versionlist $v"
done

if [ $NEWVERSION ]; then

    for v in $versionlist; do

	if [ "$NEWVERSION" == "$v" ]; then
	    pushd $FM_DIR > /dev/null
	    rm -f CurrentJDK && ln -s $NEWVERSION CurrentJDK
	    popd > /dev/null
	fi

    done

fi

current=`ls -l $FM_DIR/CurrentJDK | cut -d '>' -f 2`

if [ $CURRENT_JVM ]; then
    echo $current
else 

    echo "Available JVMs:"
    echo "---------------"

    for v in $versionlist; do

	if [ $v == $current ]; then
	    echo $v "<-- current JVM"
	else
	    echo $v
	fi

    done

fi

exit 0




